package org.example.service;

import org.example.dao.EmployeeDAO;
import org.example.entity.Employee;
import org.example.utils.DataUtils;
import org.example.utils.PingYinUtils;
import org.example.utils.TableUtils;
import org.example.utils.TimeUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;

public class EmployeeService {
    public static boolean addEmployee(String departId, String empName, String empSex, String departName) {

        //生成员工编码
        String empCode = "";
        String dep = PingYinUtils.getPinYinHeadChar(departName);
        String date = new SimpleDateFormat("yyMMdd").format(new Date()).toString();
        //从数据库中通过模糊查询查询到已有的所有对应部门的工号数据limit最后一条，
        String ordinal = EmployeeDAO.getLastEmpCode(departId);
        if (ordinal == null || "".equals(ordinal)) {
            ordinal = "01";
        } else {
            StringBuilder sb = new StringBuilder(ordinal);
            ordinal = sb.reverse().substring(0,2);
            StringBuilder sb2 = new StringBuilder(ordinal);
            Integer i = Integer.valueOf(sb2.reverse().toString());
            i++;
            ordinal = i >= 10 ? i.toString() : "0" + i;
        }
        empCode = dep + date + ordinal;

        //dao新增员工信息
        Employee employee = new Employee(
                UUID.randomUUID().toString(),
                departId,
                empName,
                empCode,
                empSex,
                TimeUtils.getTime(),
                TimeUtils.getTime());
        //dao新增
        return EmployeeDAO.addEmployee(employee);

    }

    public static boolean updateEmployee(String empNewName, String empNewSex, String empId) {
        //dao修改员工数据
        return EmployeeDAO.updateEmployee(empNewName, empNewSex, empId);

    }

    public static boolean deleteEmployee(String id) {
        return EmployeeDAO.deleteEmployee(id);
    }

    public static ArrayList<String[]> queryEmployee() {
        return EmployeeDAO.getEmployeeInfo();
    }

    //根据部门id更新员工code
    public static boolean updateEmployeeEmpCode(String dptId, String newDepartName) {
        //生成员工编码
        String empCode = "";
        String dep = PingYinUtils.getPinYinHeadChar(newDepartName);
        String date = new SimpleDateFormat("yyMMdd").format(new Date()).toString();
        //从数据库中通过模糊查询查询到已有的所有对应部门的工号数据limit最后一条，
        String ordinal = EmployeeDAO.getLastEmpCode(dptId);

        if (ordinal == null || "".equals(ordinal)) {
            ordinal = "01";
        } else {
            StringBuilder sb = new StringBuilder(ordinal);
            ordinal = sb.reverse().substring(0,2);
            StringBuilder sb2 = new StringBuilder(ordinal);
            Integer i = Integer.valueOf(sb2.reverse().toString());
            i++;
            ordinal = i >= 10 ? i.toString() : "0" + i;
        }
        empCode = dep + date + ordinal;


        return EmployeeDAO.updateEmployeeEmpCode(empCode, dptId);
    }

    public static ArrayList<String[]> getEmployeeInfo() {
        return EmployeeDAO.getEmployeeInfo();
    }
}
